package io.github.xxyopen.novel.app;


import io.github.xxyopen.novel.app.vo.ChatMemoryVo;
import io.github.xxyopen.novel.app.vo.OneChatMemoryVo;
import io.github.xxyopen.novel.core.auth.UserHolder;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
import io.github.xxyopen.novel.dao.entity.AiChatMemory;
import io.github.xxyopen.novel.service.AiChatMemoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping(ApiRouterConsts.API_FRONT_HELP_AI_PREFIX)
@Slf4j
public class AIHelperController {

    @Autowired
    AiHelperApp aiHelperApp;

    @Autowired
    AiChatMemoryService aiChatMemoryService;

    /**
     * ai对话接口
     * @param chatId 记忆对话id
     * @param message 用户的信息
     * @return
     */
    @GetMapping(value = "/ask", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> aiHelper(@RequestParam("chatId") String chatId, @RequestParam String message) {
        log.info("chatId:{}, message:{}", chatId, message);
        return aiHelperApp.chat(chatId, message);
    }


    /**
     * 查询当前登录用户的聊天记忆
     * @return
     */
    @GetMapping("/getMemoryList")
    public RestResp<List<ChatMemoryVo>> getChatMemoryList() {
        return aiChatMemoryService.getChatMemoryList();
    }

    /**
     * 根据chatId查询聊天记忆
     * @param chatId
     * @return
     */
    @GetMapping("/getMemoryByChatId")
    public RestResp<List<OneChatMemoryVo>> getChatMemoryByChatId(@RequestParam("chatId") String chatId) {
        return aiChatMemoryService.getChatMemoryByChatId(chatId);
    }


}
